import { Component, OnInit } from '@angular/core';
import { LoginInfoService } from '../../.././services/login-info.service';
import { Md5 } from "ts-md5/dist/md5";
import { HttpService } from '../../.././services/http.service';
import { BaseUiService } from '../../../services/base-ui.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-changepwd',
  templateUrl: './changepwd.page.html',
  styleUrls: ['./changepwd.page.scss'],
})
export class ChangepwdPage implements OnInit {
  public phoneNum:any
  public oldpwd:any
  public newpwd1:any
  public newpwd2:any
  public isShowpwd:boolean = true
  constructor(
    public BaseUiService: BaseUiService,
    public LoginInfoService:LoginInfoService,
    public HttpService: HttpService,
    public router: Router,
  ) { }

  ngOnInit() {
    this.phoneNum = this.LoginInfoService.getInfo().mobile
  }

  //查询原密码是否正确
  affirmpwd(){
    if(this.oldpwd == ''){
      this.BaseUiService.showToast('密码不能为空','customred','top')
    }else{
      let p = {
        loginName: this.phoneNum,
        password:Md5.hashStr(this.oldpwd)
      }
      this.HttpService.verifyPwd(p).then(res =>{
        console.log(res,'++++')
        if(res['code']==200){
          this.isShowpwd = false
        }else{
          this.BaseUiService.showToast(res['msg'],'customred','top')
        }
      }).catch(err =>{
        this.BaseUiService.showToast(err,'customred','top')
      })
    }
  }

  goforgetpwd(){
    let pwdZ = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
    if(this.newpwd1!==this.newpwd2||!this.newpwd1||!this.newpwd2){
      return this.BaseUiService.showToast('密码不一致或不能为空','customred','top')
    }else if(!pwdZ.test(this.newpwd1)){
      return this.BaseUiService.showToast('密码格式不正确,请输入6-16数字加字母','customred','top')
    }else{
      let p = {
        newPassword : Md5.hashStr(this.newpwd2),
        mobile: this.phoneNum
      }
      this.HttpService.forgetPwd(p).then(res=>{
        if(res['code']==200){
          this.BaseUiService.showToast('修改成功！','customblue','top')
          this.router.navigate(['/tabs/tab4'])
        }else{
          this.BaseUiService.showToast(res['msg'],'customred','top')
        }
      }).catch(err =>{
        this.BaseUiService.showToast(err,'customred','top')
      })
    }
  }
}
